﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ECommerce.Factory.Controles
{
    public static class WebControl
    {
        internal static void Load(ECommerce.Model.Controles.WebControl webControl, System.Data.DataRow dr)
        {
            if (dr["Tipo"] != null)
                webControl.Tipo = ECommerce.Reflection.Tipos.GetType(dr["Tipo"].ToString());
            webControl.Controles = ECommerce.DAO.Controles.WebControl.GetWebControlsByWebControlId(webControl.Id);
        }
     
        public static System.Web.UI.Control GetControlFrom(System.Data.DataRow dr)
        {
            System.Web.UI.Control result = new System.Web.UI.Control();
            if (dr["Clase"] != null)
                result = GetNuevoControlStandarfrom(dr);

            return result;
        }

        public static System.Web.UI.Control GetNuevoControlStandarfrom(System.Data.DataRow dr)
        {
            System.Web.UI.Control result = new System.Web.UI.Control();
            string Clase = dr["Clase"].ToString();
            switch (Clase)
            {
                case "ECommerce.Model.Controles.WebCompositeControl":
                    ECommerce.Model.Controles.WebCompositeControl WebCompositeControl = new ECommerce.Model.Controles.WebCompositeControl();
                    if (dr["HasChilds"] != null)
                    {
                        if (Convert.ToBoolean(dr["HasChilds"]))
                        {
                            System.Web.UI.Control[] Controles = ECommerce.DAO.SQL.Controles.WebControl.GetControlesFromCompositeControl(WebCompositeControl);
                            foreach (System.Web.UI.Control Control in Controles)
                            {
                                WebCompositeControl.Controls.Add(Control);
                            }
                        }
                    }
                    result = WebCompositeControl;
                    break;
                case "System.Web.UI.WebControls.Label":
                    System.Web.UI.WebControls.Label Label = new System.Web.UI.WebControls.Label();
                    if (dr["HasAttributes"] != null)
                        if (Convert.ToBoolean(dr["HasAttributes"]))
                        {
                            System.Web.UI.WebControls.ListItem[] Items = ECommerce.DAO.SQL.Controles.WebControl.GetAttributesFromControl(Label);
                            foreach (System.Web.UI.WebControls.ListItem Item in Items)
                            {
                                Label.Attributes.Add(Item.Text, Item.Value);
                            }
                        }
                    result = Label;
                    break;
                case "System.Web.UI.WebControls.TextBox":
                    System.Web.UI.WebControls.TextBox TextBox = new System.Web.UI.WebControls.TextBox();
                    if (dr["HasAttributes"] != null)
                        if (Convert.ToBoolean(dr["HasAttributes"]))
                        {
                            System.Web.UI.WebControls.ListItem[] Items = ECommerce.DAO.SQL.Controles.WebControl.GetAttributesFromControl(TextBox);
                            foreach (System.Web.UI.WebControls.ListItem Item in Items)
                            {
                                TextBox.Attributes.Add(Item.Text, Item.Value);
                            }
                        }
                    result = TextBox;
                    break;
                case "System.Web.UI.WebControls.Button":
                    System.Web.UI.WebControls.Button Button = new System.Web.UI.WebControls.Button();
                    if (dr["HasAttributes"] != null)
                        if (Convert.ToBoolean(dr["HasAttributes"]))
                        {
                            System.Web.UI.WebControls.ListItem[] Items = ECommerce.DAO.SQL.Controles.WebControl.GetAttributesFromControl(Button);
                            foreach (System.Web.UI.WebControls.ListItem Item in Items)
                            {
                                Button.Attributes.Add(Item.Text, Item.Value);
                            }
                        }
                    if (dr["HasEvents"]!=null)
                        if (Convert.ToBoolean(dr["HasEvents"]))
                        { 
                            System.Web.UI.WebControls.CommandEventHandler[] Events = ECommerce.DAO.SQL.Controles.WebControl.GetEventsByControl(Button);
                            foreach (System.Web.UI.WebControls.CommandEventHandler Event in Events)
                            {
                                Button.Command += Event;
                            }
                        }    
                    result = Button;
                    break;
            }
            return result;
        }

    }
}
